// pages/student/application/application.js
const app = getApp();

Page({
  data: {
    applicationInfo: {
      status: "draft",
      updateTime: "",
      result: "",
      score: 0,
      feedback: "",
      reviewer: "",
    },
    materialsList: [
      {
        type: "profile",
        title: "个人信息",
        desc: "完善个人基本资料",
        icon: "👤",
        completed: false,
      },
      {
        type: "transcript",
        title: "成绩单",
        desc: "上传并确认成绩单信息",
        icon: "📊",
        completed: false,
      },
      {
        type: "research",
        title: "科研成果",
        desc: "填报科研项目和成果",
        icon: "🔬",
        completed: false,
      },
    ],
    reviewProgress: [],
    canSubmit: false,
  },

  onLoad() {
    this.loadApplicationInfo();
    this.loadMaterialsStatus();
  },

  onShow() {
    this.loadApplicationInfo();
    this.loadMaterialsStatus();
  },

  async loadApplicationInfo() {
    try {
      const local = require("../../../utils/local");
      const result = local.getApplicationInfo();
      if (result.success) {
        this.setData({
          applicationInfo: result.data.application,
          reviewProgress: result.data.progress || [],
        });
      }
    } catch (error) {
      console.error("加载申请信息失败:", error);
    }
  },

  // 远程方法删除，改为本地

  async loadMaterialsStatus() {
    try {
      const local = require("../../../utils/local");
      const result = local.getMaterialsStatus();
      if (result.success) {
        const materialsList = this.data.materialsList.map((item) => ({
          ...item,
          completed: result.data[item.type] || false,
        }));

        const canSubmit = materialsList.every((item) => item.completed);

        this.setData({
          materialsList,
          canSubmit,
        });
      }
    } catch (error) {
      console.error("加载材料状态失败:", error);
    }
  },

  getMaterialsStatus() {
    // 已改为本地
    return { success: true, data: {} };
  },

  goToMaterial(e) {
    const type = e.currentTarget.dataset.type;
    let url = "";

    switch (type) {
      case "profile":
        url = "/pages/student/profile/edit/edit";
        break;
      case "transcript":
        url = "/pages/student/transcript/transcript";
        break;
      case "research":
        url = "/pages/student/research/research";
        break;
    }

    if (url) {
      wx.navigateTo({ url });
    }
  },

  async submitApplication() {
    if (!this.data.canSubmit) {
      wx.showToast({
        title: "请先完善所有材料",
        icon: "none",
      });
      return;
    }

    wx.showModal({
      title: "确认提交",
      content: "提交后将无法修改，确认提交申请吗？",
      success: async (res) => {
        if (res.confirm) {
          await this.doSubmitApplication();
        }
      },
    });
  },

  async doSubmitApplication() {
    wx.showLoading({ title: "提交中..." });

    try {
      const local = require("../../../utils/local");
      const result = local.submitApplication();

      if (result.success) {
        wx.showToast({
          title: "提交成功",
          icon: "success",
        });

        // 刷新申请信息
        this.loadApplicationInfo();
      } else {
        wx.showToast({
          title: result.message || "提交失败",
          icon: "error",
        });
      }
    } catch (error) {
      console.error("提交申请失败:", error);
      wx.showToast({
        title: "网络错误，请重试",
        icon: "error",
      });
    } finally {
      wx.hideLoading();
    }
  },

  // 远程提交方法删除

  viewApplication() {
    wx.navigateTo({
      url: "/pages/student/application/detail/detail",
    });
  },

  viewApplicationDetail() {
    wx.navigateTo({
      url: "/pages/student/application/detail/detail",
    });
  },
});
